package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo1 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2建立链接
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root"
        );
        System.out.println("与数据库建立链接!");
        //3创建执行语句对象Statement
        Statement state = connection.createStatement();
        /*
            在tedu库中创建一张名为userinfo的表
            CREATE TABLE userinfo(
                id INT PRIMARY KEY AUTO_INCREMENT,
                username VARCHAR(20),
                password VARCHAR(20),
                nickname VARCHAR(20),
                age INT(3)
            );
         */
        String sql= "CREATE TABLE userinfo(" +
        "                id INT PRIMARY KEY AUTO_INCREMENT, " +
        "                username VARCHAR(20), " +
        "                password VARCHAR(20), " +
        "                nickname VARCHAR(20), " +
        "                age INT(3) " +
        "            );";

        //执行sql
        state.execute(sql);
        System.out.println("执行完毕");

        //关闭连接
        connection.close();

    }
}
